package edu.fatec.controller;

import java.io.IOException;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import edu.fatec.business.UsuarioNegocio;
import edu.fatec.model.Endereco;
import edu.fatec.model.Perfil;
import edu.fatec.model.Regiao;
import edu.fatec.model.Telefone;
import edu.fatec.model.Usuario;

/**
 * Servlet implementation class ControllerUsuario
 */
@WebServlet("/ControllerUsuario")
public class ControllerUsuario extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControllerUsuario() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String comando = request.getParameter("button");
		Usuario u = new Usuario();
		Endereco e = new Endereco();
		Telefone t = new Telefone();
		Perfil p = new Perfil();
		Regiao r = new Regiao();
		
		if (comando.equalsIgnoreCase("cadastrar")){
			
			
			t.setOperadora(request.getParameter("operadora"));
			t.setDDD(Integer.parseInt(request.getParameter("ddd")));
			t.setNumero(Integer.parseInt(request.getParameter("telefone")));
			
			e.setLogradouro("logradouro");
			e.setEndereco(request.getParameter("endereco"));
			e.setBairro(request.getParameter("bairro"));
			e.setCep(request.getParameter("cep"));
			r.setEstado(request.getParameter("estado"));
			r.setCidade(request.getParameter("cidade"));
			r.setPais(request.getParameter("pais"));
			e.setNumero(Integer.parseInt(request.getParameter("numeroendereco")));
						
			u.setUsuario(request.getParameter("usuario"));
			u.setPassword(request.getParameter("senha"));
			String confirmacao = request.getParameter("confirmacaosenha");
			
			p.setEmail(request.getParameter("email"));
			p.setNome(request.getParameter("nome"));
			p.setEndereco(e);
			p.setTelefone(t);
			
			boolean vEndereco = (new Validadora<Endereco>(e)).validarObjeto();
			boolean vUsuario = (new Validadora<Usuario>(u).validarObjeto());
			boolean vTelefone = (new Validadora<Telefone>(t).validarObjeto());
			boolean vSenha = new UsuarioNegocio().validarSenha(u,confirmacao );
			
			if(vEndereco && vTelefone && vSenha && vUsuario){			
				
			}

		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("./produto.jsp");
		dispatcher.forward( request, response );
	}

	

}



